package com.ek.common;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

import com.ek.common.task.DeleteListener;
import com.ek.kademlia.protocol.Response;

public class FutureResponse<V extends Response> {
    private final BlockingQueue<V> queue;
    private final String key;
    private final Context context;

    public FutureResponse(String key, BlockingQueue<V> queue, Context context) {
        this.key = key;
        this.queue = queue;
        this.context = context;
    }

    public V get(long timeout, TimeUnit unit) {
        try {
            V poll = queue.poll(timeout, unit);
            context.getTaskManager().put(new DeleteListener(key, context.getReceivedPacketHandler()));
            return poll;
        } catch (InterruptedException e) {

        }
        return null;
    }

}
